package net.lab1024.sa.admin.module.business.category.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import net.lab1024.sa.admin.constant.AdminSwaggerTagConst;
import net.lab1024.sa.admin.module.business.category.domain.dto.RecordQueryDTO;
import net.lab1024.sa.admin.module.business.category.domain.entity.*;
import net.lab1024.sa.admin.module.business.category.domain.vo.SynthesisRecordVO;
import net.lab1024.sa.admin.module.business.category.service.SynthesisRecordService;
import net.lab1024.sa.base.common.code.SynthesisErrorCode;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static net.lab1024.sa.base.common.domain.ResponseDTO.OK_CODE;

@Tag(name = AdminSwaggerTagConst.Business.SYNTHESIS_RECORD)
@RestController
@RequestMapping("/api/synthesis/")
@RequiredArgsConstructor
public class RecordController {
    private final SynthesisRecordService recordService;

    // 分页查询
    @Operation(summary = "查询记录列表(分页查询)")
    @PostMapping("list")
    public Page<SynthesisRecordVO> listRecords(@RequestBody @Valid RecordQueryDTO query) {
        Page<SynthesisRecordVO> pageResult = recordService.listRecords(query);
        return pageResult;
    }

    @PostMapping("/analyze")
    public ResponseDTO<RetroResponse.RetroResult2> analyze(
            @RequestBody SynthesisRequest request) {
        try {
            RetroResponse.RetroResult2 result = recordService.analyze(request);
            return ResponseDTO.ok(result);
        } catch (Exception e) {
            return ResponseDTO.error(SynthesisErrorCode.ANALYZE_SMILES, e.getMessage());
        }
    }

    // 查询所有数据
    @Operation(summary = "查询记录列表(查询所有)")
    @PostMapping("all")
    public ResponseDTO<List<SynthesisRecordVO>> listAllRecords(@RequestBody @Valid RecordQueryDTO query) {
        return ResponseDTO.ok(recordService.listAllRecords(query));
    }

    @Operation(summary = "查询单条记录")
    @GetMapping("get/{id}")
    public ResponseDTO<SynthesisRecordVO> getRecord(@PathVariable @Parameter(description = "记录的唯一标识符", example = "123") Long id) {
        return ResponseDTO.ok(recordService.getRecordById(id));
    }

    @Operation(summary = "新增记录")
    @PostMapping("create")
    public ResponseDTO<Long> createRecord(@RequestBody @Valid SynthesisRecord record) {
        try {
            Long createResult = recordService.createRecord(record);
            return ResponseDTO.ok(createResult);
        } catch (Exception e) {
            return new ResponseDTO<>(OK_CODE, null, false, "新增失败", null);
        }
    }

    @Operation(summary = "更新记录")
    @PutMapping("update/{id}")
    public ResponseDTO<Boolean> updateRecord(@PathVariable @Parameter(description = "记录的唯一标识符", example = "123") Long id,
                                             @RequestBody @Valid SynthesisRecord record) {
        try {
            record.setId(id);
            boolean updateResult = recordService.updateRecord(record);
            return ResponseDTO.ok(updateResult);
        } catch (Exception e) {
            return ResponseDTO.error(SynthesisErrorCode.UPDATE_RECORD, e.getMessage());
        }
    }

    @Operation(summary = "删除记录")
    @DeleteMapping("delete/{id}")
    public ResponseDTO<Boolean> deleteRecord(@PathVariable @Parameter(description = "记录的唯一标识符", example = "123") Long id) {
        try {
            boolean deleteResult = recordService.deleteRecord(id);
            return ResponseDTO.ok(deleteResult);
        } catch (Exception e) {
            return ResponseDTO.ok(false);
        }
    }

}